var r = {
    getObjKeys: function(r) {
        if (r !== Object(r)) throw new TypeError("Invalid object");
        var t = [];
        for (var e in r) Object.prototype.hasOwnProperty.call(r, e) && (t[t.length] = e);
        return t;
    },
    combInArray: function(t) {
        if (!t || !t.length) return [];
        for (var e = t.length, o = [], i = 1; i < e; i++) for (var n = r.getCombFlags(e, i); n.length; ) {
            for (var a = n.shift(), l = [], u = 0; u < e; u++) a[u] && l.push(t[u]);
            o.push(l);
        }
        return o;
    },
    getCombFlags: function(r, t) {
        if (!t || t < 1) return [];
        var e = [], o = [], i = !0, n = void 0, a = void 0, l = void 0;
        for (n = 0; n < r; n++) o[n] = n < t ? 1 : 0;
        for (e.push(o.concat()); i; ) for (l = 0, n = 0; n < r - 1; n++) {
            if (1 == o[n] && 0 == o[n + 1]) {
                for (a = 0; a < n; a++) o[a] = a < l ? 1 : 0;
                o[n] = 0, o[n + 1] = 1;
                var u = o.concat();
                e.push(u), -1 == u.slice(-t).join("").indexOf("0") && (i = !1);
                break;
            }
            1 == o[n] && l++;
        }
        return e;
    },
    initSKU: function(t, e, o) {
        for (var i = {}, n = r.getObjKeys(t), a = 0; a < n.length; a++) {
            var l = n[a], u = t[l], f = l.split(","), c = [], v = !0, s = !1, y = void 0;
            try {
                for (var h, p = e[Symbol.iterator](); !(v = (h = p.next()).done); v = !0) {
                    var d = h.value, b = !0, k = !1, g = void 0;
                    try {
                        for (var m, S = f[Symbol.iterator](); !(b = (m = S.next()).done); b = !0) {
                            var w = m.value;
                            if (!w) break;
                            if (w.split(o)[0] === d) {
                                c.push(w);
                                break;
                            }
                        }
                    } catch (r) {
                        k = !0, g = r;
                    } finally {
                        try {
                            !b && S.return && S.return();
                        } finally {
                            if (k) throw g;
                        }
                    }
                }
            } catch (r) {
                s = !0, y = r;
            } finally {
                try {
                    !v && p.return && p.return();
                } finally {
                    if (s) throw y;
                }
            }
            f = c;
            for (var x = r.combInArray(f), j = 0; j < x.length; j++) {
                var _ = x[j].join(",");
                i[_] ? (i[_].stock += u.stock, i[_].prices.push(1 * u.price)) : i[_] = {
                    stock: u.stock,
                    prices: [ 1 * u.price ]
                };
            }
            i[f.join(",")] = {
                stock: u.stock,
                prices: [ 1 * u.price ]
            };
        }
        return i;
    },
    getCantSelectedItems: function(r, t, e, o) {
        var i = {}, n = [];
        for (var a in r) r[a] && (o[a + "/" + r[a]] ? n.push(a + "/" + r[a]) : r[a] = null);
        if (n.length) {
            var l = JSON.parse(JSON.stringify(e)), u = !0, f = !1, c = void 0;
            try {
                for (var v, s = n[Symbol.iterator](); !(u = (v = s.next()).done); u = !0) {
                    var y = v.value;
                    e[y] && delete l[y];
                }
            } catch (r) {
                f = !0, c = r;
            } finally {
                try {
                    !u && s.return && s.return();
                } finally {
                    if (f) throw c;
                }
            }
            for (var h in l) {
                var p = [], d = "", b = !0, k = !1, g = void 0;
                try {
                    for (var m, S = n[Symbol.iterator](); !(b = (m = S.next()).done); b = !0) {
                        var w = m.value;
                        if (w = w.split("/"), h.split("/")[0] === w[0]) {
                            d = w.join("/");
                            break;
                        }
                    }
                } catch (r) {
                    k = !0, g = r;
                } finally {
                    try {
                        !b && S.return && S.return();
                    } finally {
                        if (k) throw g;
                    }
                }
                if (d) {
                    var x = !0, j = !1, _ = void 0;
                    try {
                        for (var O, R = n[Symbol.iterator](); !(x = (O = R.next()).done); x = !0) {
                            var I = O.value;
                            I !== d && p.push(I);
                        }
                    } catch (r) {
                        j = !0, _ = r;
                    } finally {
                        try {
                            !x && R.return && R.return();
                        } finally {
                            if (j) throw _;
                        }
                    }
                } else p = n.concat([]);
                p = p.concat(h);
                var z = [], K = !0, A = !1, C = void 0;
                try {
                    for (var F, J = t[Symbol.iterator](); !(K = (F = J.next()).done); K = !0) {
                        var N = F.value, D = !0, E = !1, P = void 0;
                        try {
                            for (var T, U = p[Symbol.iterator](); !(D = (T = U.next()).done); D = !0) {
                                var q = T.value;
                                if (q.split("/")[0] === N) {
                                    z.push(q);
                                    break;
                                }
                            }
                        } catch (r) {
                            E = !0, P = r;
                        } finally {
                            try {
                                !D && U.return && U.return();
                            } finally {
                                if (E) throw P;
                            }
                        }
                    }
                } catch (r) {
                    A = !0, C = r;
                } finally {
                    try {
                        !K && J.return && J.return();
                    } finally {
                        if (A) throw C;
                    }
                }
                o[z.join(",")] && 1 * o[h].stock != 0 ? delete i[h] : i[h] = 1;
            }
        } else for (var B in e) o[B] && 1 * o[B].stock != 0 || (i[B] = 1);
        return {
            cantSelectedItems: i,
            curAttr: r
        };
    },
    removeSkuRepeat: function(r) {
        if (!r || !r.length) return [];
        var t = [], e = {}, o = !0, i = !1, n = void 0;
        try {
            for (var a, l = r[Symbol.iterator](); !(o = (a = l.next()).done); o = !0) {
                var u = a.value;
                e[u.id] || (t.push(u), e[u.id] = u);
            }
        } catch (r) {
            i = !0, n = r;
        } finally {
            try {
                !o && l.return && l.return();
            } finally {
                if (i) throw n;
            }
        }
        return t.sort(function(r, t) {
            return r.id - t.id;
        });
    },
    getSkuResult: function(t, e) {
        if (!t || !t.length) return {};
        var o = {}, i = [], n = [], a = [], l = [], u = !0, f = !1, c = void 0;
        try {
            for (var v, s = t[Symbol.iterator](); !(u = (v = s.next()).done); u = !0) {
                var y = v.value;
                y.print_position === 1 * e && (o["clothing/" + y.goods_id + ",color/" + y.color_id + ",type/" + y.style_id + ",size/" + y.size_id] = {
                    price: y.price,
                    stock: y.stock
                }, i.push(y.goods_list), n.push(y.color_list), a.push(y.style_list), l.push(y.size_list));
            }
        } catch (r) {
            f = !0, c = r;
        } finally {
            try {
                !u && s.return && s.return();
            } finally {
                if (f) throw c;
            }
        }
        return {
            skuResult: o,
            skuKeyData: [ {
                title: "款式",
                attr: r.removeSkuRepeat(i),
                type: "clothing"
            }, {
                title: "颜色",
                attr: r.removeSkuRepeat(n),
                type: "color"
            }, {
                title: "类型",
                attr: r.removeSkuRepeat(a),
                type: "type"
            }, {
                title: "尺码",
                attr: r.removeSkuRepeat(l),
                type: "size"
            } ]
        };
    }
};

module.exports = r;